package cn.com.headfree.util;

import cn.com.headfree.base.common.MyConstant;
import cn.com.headfree.base.service.SysDictionaryInfoService;
import cn.com.headfree.vo.SysDictionaryInfoVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gta.edu.sdk.util.EhCacheUtil;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 字典工具类
 */
@Component
public class SysDictionaryUtil {

    private static final Logger log = LogManager.getLogger(SysDictionaryUtil.class);

    private static Map<String, List<SysDictionaryInfoVo>> staticMap = new HashMap<>();

//    @Autowired
//    private SysDictionaryInfoService sevice;

//    private static SysDictionaryInfoService sysDictionaryInfoService;
//    private static int dictionaryTime;
//    private static String pid;

//    @Value("${redis.dictionary.timelong}")
//    public void setDictionaryTime(int dictionaryTime) {
//        this.dictionaryTime = dictionaryTime;
//    }

//    @PostConstruct
//    public void init() {
//        sysDictionaryInfoService = sevice;
//    }

    /*
     * @Description: 获取字典值
     * @Param: [value, dictName]
     * @return: java.lang.String
     * @Author: panl
     * @createTime: 2020/7/5 16:55
     */
    public static String getValueByValueAndName(String value, String dictName) {
        Map<String, List<SysDictionaryInfoVo>> map = getSysDictionaryList();
        List<SysDictionaryInfoVo> entityList = map.get(value);
        String dictValue = "";
        if(dictName != null){
            if (entityList != null && entityList.size() > 0) {
                for (int i = 0; i < entityList.size(); i++) {
                    SysDictionaryInfoVo entity = JSONObject.parseObject(JSON.toJSONString(entityList.get(i)), SysDictionaryInfoVo.class);
                    if (dictName.equals(entity.getName())) {
                        dictValue = entity.getValue();
                        return dictValue;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取name值
     * @param name
     * @param dictValue
     * @return
     */
    public static String getNameByValue(String name,String dictValue){
        Map<String, List<SysDictionaryInfoVo>> map = getSysDictionaryList();
        if(map != null && map.size() > 0){
            List<SysDictionaryInfoVo> entityList = map.get(name);
            String dictName = "";
            if(dictValue != null){
                if (entityList != null && entityList.size() > 0) {
                    for (int i = 0; i < entityList.size(); i++) {
                        SysDictionaryInfoVo entity = JSONObject.parseObject(JSON.toJSONString(entityList.get(i)), SysDictionaryInfoVo.class);
                        if (dictValue.equals(entity.getValue())) {
                            dictName = entity.getName();
                            return dictName;
                        }
                    }
                }
            }
        }
        return "";
    }

    public static String getNameByValueForDefault(String name,String dictValue){
        String value = getNameByValue(name,dictValue);
        if(StringUtils.isEmpty(value)){
            value = dictValue;
        }
        return value;
    }


    /*
     * @Description: 字典数据Redis获取、存储
     * @Param: []
     * @return: java.util.Map<java.lang.String,java.util.List<cn.com.headfree.vo.SysDictionaryInfoVo>>
     * @Author: panl
     * @createTime: 2020/7/5 16:54
     */
    public static Map<String, List<SysDictionaryInfoVo>> getSysDictionaryList() {
        Map<String, List<SysDictionaryInfoVo>> codeMap = staticMap;
        if (codeMap != null && codeMap.size() > 0) {
            return codeMap;
        }
        return codeMap;
    }

    public static void initSysDictionaryInfo(WebApplicationContext app) {
        Map<String, List<SysDictionaryInfoVo>> codeMap = new HashMap<>();
        SysDictionaryInfoService sysDictionaryInfoService = (SysDictionaryInfoService)app.getBean("sysDictionaryInfoService");
        List<SysDictionaryInfoVo> dictList = sysDictionaryInfoService.getSysDictionaryInfo("6d81ed6c864046ffb564e78912ab2c10");
        for (SysDictionaryInfoVo code : dictList) {
            List<SysDictionaryInfoVo> entityList = sysDictionaryInfoService.getSysDictionaryInfo(code.getId());
            staticMap.put(code.getValue(), entityList);
        }
    }

}
